extends IEventBehavior


class_name HitSplit
"""
分裂效果
"""


## 是否附加效果
@export var is_apply_effect: bool = false
## 分裂的角度
@export_range(0, 180, 1, "radians") var split_angle: float = deg_to_rad(10)
## 分裂的发射距离
@export var split_distance: float = 0
## 分裂的发射偏移位置
@export var split_muzzle: Vector2 = Vector2.ZERO
## 固定的发射位置
@export var fixed_angles: Array[int] = []


func support() -> Array:
    return [0, CharacterUnit.Event.ON_HIT_EXECUTE]

## 处理子弹初始化事件
func do_event(character: CharacterComponent, v: EffectValue, e: CharacterEvent) -> void:
    var source: ProjectileResource = e.meta.source
    var angles: Array[float] = []
    if v.value > 1 and split_angle > 0:
        angles.append_array(ShapeDraw.get_split_angles(v.value, split_angle))
    for d in fixed_angles:
        angles.append(deg_to_rad(d))
    var start_angle: float = source.start_direction.angle()
    var start_position: Vector2 = source.start_position + split_muzzle
    var projectile: Projectile = e.meta.projectile
    for i in range(len(angles)):
        var value: float = angles[i] + start_angle
        source.start_direction = Vector2.from_angle(value)
        source.start_position = start_position
        if split_distance > 0:
            source.start_position += split_distance * source.start_direction
        var b: Projectile = projectile.weapon.spawn(source)
        if is_apply_effect:
            b.processor = projectile.processor.clone()
    e.cancel()
